<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Upload extends CI_Controller{
	
	/**
	 * @todo 商品图片1
	 */
	function goods1()
	{
		try{
			$updata = $this->image("goods_img1");			
			echo "<script>window.parent.goods1_success('".$updata['url']."');</script>";
		}catch(Exception $err){
			echo "<script>window.parent.goods1_failed('".$err->getMessage()."')</script>";
		}		
	}	
	/**
	 * @todo 商品图片2
	 */
	function goods2()
	{
		try{
			$updata = $this->image("goods_img2");
			echo "<script>window.parent.goods2_success('".$updata['url']."');</script>";
		}catch(Exception $err){
			echo "<script>window.parent.goods2_failed('".$err->getMessage()."')</script>";
		}
	}
	/**
	 * 商品图片3
	 */
	function goods3()
	{
		try{
			$updata = $this->image("goods_img3");
			echo "<script>window.parent.goods3_success('".$updata['url']."');</script>";
		}catch(Exception $err){
			echo "<script>window.parent.goods3_failed('".$err->getMessage()."')</script>";
		}
	}
	/**
	 * 商品图片4
	 */
	function goods4()
	{
		try{
			$updata = $this->image("goods_img4");
			echo "<script>window.parent.goods4_success('".$updata['url']."');</script>";
		}catch(Exception $err){
			echo "<script>window.parent.goods4_failed('".$err->getMessage()."')</script>";
		}
	}
	/**
	 * @todo 商品分类图片
	 */
	function catimg(){
		try{
			$updata = $this->image("catimg");
			echo "<script>window.parent.img_success('".$updata['url']."');</script>";
		}catch(Exception $err){
			echo "<script>window.parent.img_failed('".$err->getMessage()."')</script>";
		}
	}
	/**
	 * @todo 图片上传公共函数  支持图片缩图
	 * @param unknown_type $file 文件控件名称
	 * @param unknown_type $resize 是否缩图
	 * @param unknown_type $width  缩图宽度
	 * @param unknown_type $height 缩图高度
	 * @throws Exception
	 * @return string
	 */
	protected function image($file,$resize=false,$width=null,$height=null){		
		$this->load->library('upload');		
		if(!$this->upload->do_upload($file)){
			throw new Exception($this->upload->display_errors());			
		}
		if($resize && $width && $height){
			
		}
		$data = $this->upload->data();
		$baseUrl = $this->config->item("base_url");
		
		$data['url'] = $baseUrl."upload/temp/". $data['file_name']; 
		return $data;		
	}
	protected function resize(){
		//图片裁剪				
		$config['source_image'] = "./upload/temp/4.gif";		
		$config['width'] = 75;
		$config['height'] = 50;			
		$this->load->library('image_lib');
		$this->image_lib->initialize($config);		
		if ( ! $this->image_lib->resize())
		{
			echo $this->image_lib->display_errors();
		}
	}
}